package rsj.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import rsj.netty.conf.GatawayConfig;
import rsj.netty.inithandler.InitServerHandler;

/**
 * @author xuguoliang@rlyy.com
 * @date 2019/12/19 17:09
 * @description
 * @package ${PACKAGE}
 */

@Configuration
@Component
public class NettyServer implements ApplicationListener<ApplicationStartedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(NettyServer.class);

    @Autowired
    private GatawayConfig gatawayConfig;

    @Override
    public void onApplicationEvent(@NonNull ApplicationStartedEvent event) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup();
        ChannelFuture channelFuture = null;
        try {
            bootstrap.group(boss,worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(
                            new InitServerHandler());
            channelFuture = bootstrap.bind(gatawayConfig.getPort()).syncUninterruptibly().addListener(future -> {
                String logBanner = "\n\n" +
                        "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" +
                        "*                                                                                   *\n" +
                        "*                                                                                   *\n" +
                        "*                   Netty Http Server started on port {"+ gatawayConfig.getPort() +" }                       *\n" +
                        "*                                                                                   *\n" +
                        "*                                                                                   *\n" +
                        "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n";
                logger.info(logBanner);
            });
            System.out.println("SpringBoot + Netty 本地开放PORT:" + gatawayConfig.getPort());
          //  cf.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            channelFuture.channel().closeFuture().addListener(future -> {
                //关闭loopGroup
                boss.shutdownGracefully();
                worker.shutdownGracefully();
            });
        }
    }



}
